<template>
  <div id="app">
    <!-- 路由占位符 -->
    <router-view></router-view>
    <div @click="backTop" class="goTop">Top</div>
  </div>
</template>

<script>
export default {
  data () {
    return {

    }
  },
  created () {},
  mounted () {
    window.addEventListener('scroll', this.showBtn, true)
  },
  methods: {
    showBtn () {
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      this.scrollTop = scrollTop
    },
    backTop () {
      var timer = setInterval(function () {
        const osTop = document.documentElement.scrollTop || document.body.scrollTop
        const ispeed = Math.floor(-osTop / 5)
        document.documentElement.scrollTop = document.body.scrollTop = osTop + ispeed
        this.isTop = true
        if (osTop === 0) {
          clearInterval(timer)
        }
      }, 30)
      window.onmousewheel = scorllFn
      function scorllFn (e) {
        e = window.event || e
        if (e.wheelDelta < 0) {
          clearInterval(timer)
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>

// 回到顶部
.goTop {
  position: fixed;
  bottom: 40px;
  right: 40px;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 50%;
  background-color: #409eff;
  color: #fff;
  cursor: pointer;
}
</style>
